/**
 * 
 */
package com.qc.ds.controller.backstage;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qc.ds.pojo.AdminInfo;
import com.qc.ds.pojo.ProductDetails;
import com.qc.ds.pojo.ProductImg;
import com.qc.ds.services.AddProducesService;
import com.qc.ds.util.Const;
import com.qc.ds.util.DateFormatUtil;
import com.qc.ds.util.PropertieUtil;
import com.qc.ds.vo.AddProductVo;

@Controller
@RequestMapping("manager/product/")
public class AddProduct {

	@Autowired
	private AddProducesService addProducesService;

	@RequestMapping(value = "addProduct")
	public void addProduct(AddProductVo addProductVo, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) {
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (session.getAttribute(Const.USER_INFO) == null) {
			out.write("<script>alert('没有登录，请先登录');location='../../admin/login.jsp';</script>");
		} else {
			// 获取当前用户Id
			AdminInfo userInfo = (AdminInfo) session.getAttribute(Const.USER_INFO);
			Integer userId = userInfo.getId();
			// 获取商品Id
			String productId = this.getProductId();
			addProductVo.setProductId(productId);
			// 1.保存商品基本信息
			addProducesService.addProduce(addProductVo);
			// 获取商品图片
			String imgPath = addProductVo.getBigImgPath();
			// 获取商品详情
			String productDetails = addProductVo.getProductDetails();
			// 获得文件发布路径
			String webPath = request.getSession().getServletContext().getRealPath("/");
			// 获取要保存的绝对路径
			String savePath = PropertieUtil.getProperty("propertiesImgPath", "D:\\HuaWeiImg");
			// 动态获取日期格式的文件名
			String fileName = DateFormatUtil.getDateFormat("yyyyMMdd");
			// 大图片保存路径；
			List<ProductImg> maxImgList = null;
			List<ProductDetails> detailsList=null;
			Boolean addProductImg = false;
			Boolean addProductDetails = false;
			// 获取文件拷贝后的路径
			if (null != imgPath&&!"".equals(imgPath)) {
				maxImgList = addProducesService.getSavePath(imgPath, savePath, fileName, webPath, productId, userId);
				// 2.保存商品图片信息
				addProductImg = addProducesService.addProductImg(maxImgList);
			}else{
				addProductImg= true;
			}
			if (null != productDetails&&!"".equals(productDetails)) {
				detailsList = addProducesService.getDetailsPath(productDetails, savePath, fileName, webPath,productId, userId);
				// 3. 保存商品详情信息
				addProductDetails = addProducesService.addProductDetails(detailsList);
			}else{
				addProductDetails=true;
			}
			
			// 清除tomcat里的图片
			File file = new File(webPath);
			File[] fi = file.listFiles();
			for (File f : fi) {
				if (f.isFile()) {
					f.delete();
				}
			}
			
			if (addProductImg && addProductDetails) {
				out.write("<script>alert('添加成功！');location='../../admin/index.jsp'</script>");
			} else {
				out.write("<script>alert('添加失败！');location='../../admin/index.jsp'</script>");
			}
		}
	}

	/**
	 * @date 2017年8月5日) 下午6:15:34
	 * @Description: TODO 动态获得25位商品Id[日期+毫秒值]
	 * @return
	 */
	public String getProductId() {
		int id = (int) (Math.random() * 90000000 + 10000000);
		return DateFormatUtil.getDateFormat("yyyyMMddHHmmssSS") + String.valueOf(id);
	}
}
